#!/bin/bash

# ==============================================
# AutoPack 跨平台一键构建脚本
# 支持 Windows (Git Bash/MSYS2) 和 Ubuntu/Linux 系统
# 使用方法: ./build.sh
# ==============================================

set -e

PROJECT_NAME="AutoPack"
BUILD_DIR="build"

# 检测操作系统
OS="unknown"
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    OS="ubuntu"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "win32" ]]; then
    OS="win"
elif [[ "$(uname -s)" == "MINGW"* ]] || [[ "$(uname -s)" == "MSYS"* ]]; then
    OS="win"
fi

echo "=========================================="
echo "     AutoPack CMake 构建脚本"
echo "     检测到系统: $OS"
echo "=========================================="
echo ""

# 检查依赖
echo "🔍 检查构建依赖..."
if ! command -v cmake >/dev/null 2>&1; then
    echo "❌ 错误：未找到 cmake，请先安装"
    if [ "$OS" = "ubuntu" ]; then
        echo "Ubuntu: sudo apt install cmake"
    else
        echo "Windows: 请从 https://cmake.org 下载安装"
    fi
    exit 1
fi

# 根据系统检查构建工具
if [ "$OS" = "win" ]; then
    # Windows下检查构建工具
    if ! command -v msbuild >/dev/null 2>&1 && ! command -v make >/dev/null 2>&1 && ! command -v ninja >/dev/null 2>&1; then
        echo "❌ 错误：未找到构建工具 (MSBuild/Make/Ninja)"
        echo "Windows: 请安装 Visual Studio 或 MSYS2"
        exit 1
    fi
else
    # Linux下检查make
    if ! command -v make >/dev/null 2>&1; then
        echo "❌ 错误：未找到 make，请先安装"
        echo "Ubuntu: sudo apt install build-essential"
        exit 1
    fi
fi

echo "✅ 构建依赖检查通过"
echo ""

# [1/4] 准备构建目录
echo "[1/4] 准备构建目录..."
if [ -d "$BUILD_DIR" ]; then
    echo "清理旧的构建目录..."
    rm -rf "$BUILD_DIR"
fi
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"

echo "✅ 构建目录已准备"
echo ""

# [2/4] 配置CMake项目
echo "[2/4] 配置CMake项目..."
if [ "$OS" = "win" ]; then
    # Windows下尝试使用Visual Studio生成器
    cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release ..
else
    cmake -DCMAKE_BUILD_TYPE=Release ..
fi

if [ $? -ne 0 ]; then
    echo "❌ CMake配置失败！"
    exit 1
fi

echo "✅ CMake配置成功"
echo ""

# [3/4] 构建项目
echo "[3/4] 构建项目..."
if [ "$OS" = "win" ]; then
    # Windows下使用cmake --build
    make -j$(nproc)
else
    # Linux下使用make
    if command -v nproc >/dev/null 2>&1; then
        make -j$(nproc)
    else
        make -j4
    fi
fi

if [ $? -ne 0 ]; then
    echo "❌ 构建失败！"
    exit 1
fi

echo "✅ 项目构建成功"
echo ""

# [4/4] 显示结果
echo "[4/4] 构建完成！"
echo ""
echo "=========================================="
echo "构建结果："

# 查找生成的可执行文件
EXECUTABLE=""
EXE_SUFFIX=""
if [ "$OS" = "win" ]; then
    EXE_SUFFIX=".exe"
fi

# 按优先级查找可执行文件
SEARCH_PATHS=(
    "$PROJECT_NAME/$PROJECT_NAME$EXE_SUFFIX"
    "AutoPack/$PROJECT_NAME$EXE_SUFFIX"
    "$PROJECT_NAME$EXE_SUFFIX"
    "Release/$PROJECT_NAME$EXE_SUFFIX" 
    "Debug/$PROJECT_NAME$EXE_SUFFIX"
    "$PROJECT_NAME.exe"
    "AutoPack/$PROJECT_NAME.exe"
    "Release/$PROJECT_NAME.exe"
    "Debug/$PROJECT_NAME.exe"
)

for path in "${SEARCH_PATHS[@]}"; do
    if [ -f "$path" ]; then
        EXECUTABLE="$path"
        break
    fi
done

if [ -n "$EXECUTABLE" ]; then
    echo "✅ 可执行文件: $(pwd)/$EXECUTABLE"
    echo "🎯 系统平台: $OS"
    echo "=========================================="
else
    echo "❌ 未找到可执行文件"
    echo "� 当前目录内容:"
    ls -la
    exit 1
fi

echo ""
echo "🎉 构建脚本执行完成！"
